package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.behavior.dto.EntryDto;
import com.heima.behavior.dto.ReadBehaviorDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.entity.ApReadBehavior;
import com.heima.behavior.mapper.ApReadBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApReadBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.AppUserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP阅读行为表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2021-05-29
 */
@Service
public class ApReadBehaviorServiceImpl extends ServiceImpl<ApReadBehaviorMapper, ApReadBehavior> implements IApReadBehaviorService {

    @Autowired
    private IApBehaviorEntryService iApBehaviorEntryService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResponseResult saveReadBehavior(ReadBehaviorDto dto) {
        //构建Entry对象
        EntryDto entryDto = new EntryDto();
        //查询用户是否登录
        User user = AppUserThreadLocal.get();
        if (user != null) {
            entryDto.setUserId(user.getUserId());
        }
        entryDto.setEquipmentId(dto.getEquipmentId());

        ApBehaviorEntry entry = iApBehaviorEntryService.getEntry(entryDto);

        LambdaQueryWrapper<ApReadBehavior> query = new LambdaQueryWrapper<>();
        query.eq(ApReadBehavior::getEntryId, entry.getId());
        query.eq(ApReadBehavior::getArticleId, dto.getArticleId());
        ApReadBehavior readBehavior = this.getOne(query);
        if (readBehavior == null) {
            ApReadBehavior apReadBehavior = new ApReadBehavior();
            apReadBehavior.setEntryId(entry.getId());
            apReadBehavior.setArticleId(dto.getArticleId());
            apReadBehavior.setCount(1);
            apReadBehavior.setReadDuration(dto.getReadDuration());
            apReadBehavior.setPercentage(dto.getPercentage());
            apReadBehavior.setLoadDuration(dto.getLoadDuration());
            apReadBehavior.setCreatedTime(new Date());
            this.save(apReadBehavior);
        } else {
            LambdaUpdateWrapper<ApReadBehavior> update = new LambdaUpdateWrapper<>();
            update.eq(ApReadBehavior::getId, readBehavior.getId());
            update.set(ApReadBehavior::getLoadDuration, dto.getLoadDuration());
            update.set(ApReadBehavior::getReadDuration, dto.getReadDuration());
            update.set(ApReadBehavior::getPercentage, dto.getPercentage());
            update.set(ApReadBehavior::getUpdatedTime, new Date());
            update.setSql("count = count+1");
            this.update(update);
        }
        return ResponseResult.okResult();
    }
}
